package com.jkoss.action.kc;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import com.jkoss.biz.kc.couBiz;
import com.jkoss.biz.kc.coursebiz;
import com.jkoss.biz.kc.qtionbiz;

import com.jkoss.pojo.kc.Course;
import com.jkoss.pojo.kc.Coursemodel;
import com.jkoss.pojo.kc.Question;
import com.jkoss.pojo.kc.Subject;
import com.jkoss.tool.Page;

@Controller
@RequestMapping("/kc")
public class CoursemodelAction {

	@Autowired
	private coursebiz cbiz;
	
	
	@Autowired
	private couBiz cobiz;

	@RequestMapping("/showAllCourse.do")
	public String showcoursemodel( HttpServletRequest req,Page<Coursemodel> page,Integer kcid){
		
		if(page==null){
			page=new Page<Coursemodel>();
		}
		  
          page.setResults(cbiz.findListCourse(page,kcid));
		  req.setAttribute("page", page);
		
		
		List<Course> courses=cobiz.listByCou();
		req.setAttribute("kcid", kcid);
		req.setAttribute("courses", courses);
		
		
		return "listckm.jsp";
	}
	
	@RequestMapping("/oneKcmoder.do")
	public String CheckOneKcmodel(Integer kcmdelid ,HttpServletRequest req){
		
		Coursemodel csm =cbiz.findKcByOne(kcmdelid);
		
		if(csm!=null){
			req.setAttribute("csm", csm);
		}
		return "shKC.jsp";
	}
	
	@RequestMapping(value="/ajOnekcm.do",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String toUpdateKCmodel(Integer kcmID){
		
		return   JSONObject.toJSONString(cbiz.findKcByOne(kcmID)).toString();
	}
	
	@RequestMapping("upKC.do")
	public String UpdateKCmodel(Coursemodel csm ,HttpServletRequest req){
	
		int row=cbiz.upkcmodel(csm);
		
		if(row>0){
			req.setAttribute("msg", "修改成功");
		}else{
			req.setAttribute("msg", "修改失败");
		}
		
		return showcoursemodel(req,null,null);
	}
	
	@RequestMapping("addKC.do")
	public String addKCModel(Coursemodel csm,HttpServletRequest req){
		
		
		Coursemodel  c=cbiz.findTheSame(csm.getKcid(),csm.getKcmdelName());
		
		
		if(c!=null){
			req.setAttribute("msg", "添加失败，已存在"+csm.getKcmdelName()+"模块");
			return showcoursemodel(req,null,null);
		}
		
	   int row = cbiz.addkcmodel(csm);
	   if(row>0){
			req.setAttribute("msg", "添加成功");
		}else{
			req.setAttribute("msg", "添加失败");
		}
		return showcoursemodel(req,null,null);
	}
	@RequestMapping("delKC.do")
	public String delKCModel(String KCModelID, HttpServletRequest req) {

		int row = cbiz.delkcmodel(KCModelID);

		if (row > 0) {
			req.setAttribute("msg", "删除成功");
		} else {
			req.setAttribute("msg", "删除失败");
		}

		return showcoursemodel(req,null,null);

	}

	
	
}